package com.cr.business.service.impl;

import com.cr.business.domain.TbProductInfo;
import com.cr.business.domain.dto.ProductReqDto;
import com.cr.business.domain.vo.AppProductOptionData;
import com.cr.business.mapper.TbProductInfoMapper;
import com.cr.business.service.ITbProductInfoService;
import com.cr.common.exception.ServiceException;
import com.cr.common.utils.DateUtils;
import com.cr.common.utils.uuid.IdUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 产品信息Service业务层处理
 * 
 * @author tang
 * @date 2025-08-19
 */
@Service
public class TbProductInfoServiceImpl implements ITbProductInfoService 
{
    @Resource
    private TbProductInfoMapper tbProductInfoMapper;

    @Override
    public List<TbProductInfo> selectTbProductInfoList(ProductReqDto dto)
    {
        return tbProductInfoMapper.selectTbProductInfoList(dto);
    }

    /**
     * 新增产品信息
     * 
     * @param tbProductInfo 产品信息
     * @return 结果
     */
    @Override
    public int insertTbProductInfo(TbProductInfo tbProductInfo)
    {

        TbProductInfo info = tbProductInfoMapper.selectByName(tbProductInfo.getProName());
        if(info != null) {
            throw new ServiceException("该产品名称已存在！");
        }

        Date nowDate = DateUtils.getNowDate();

        tbProductInfo.setProId(IdUtils.fastSimpleUUID());
        tbProductInfo.setCreatorType("0");
        tbProductInfo.setCreateTime(nowDate);
        tbProductInfo.setUpdateTime(nowDate);
        return tbProductInfoMapper.insertTbProductInfo(tbProductInfo);
    }

    /**
     * 修改产品信息
     * 
     * @param tbProductInfo 产品信息
     * @return 结果
     */
    @Override
    public int updateTbProductInfo(TbProductInfo tbProductInfo)
    {
        tbProductInfo.setUpdateTime(DateUtils.getNowDate());
        return tbProductInfoMapper.updateTbProductInfo(tbProductInfo);
    }

    /**
     * 批量删除产品信息
     * 
     * @param proIds 需要删除的产品信息主键
     * @return 结果
     */
    @Override
    public int deleteTbProductInfoByProIds(String[] proIds)
    {
        return tbProductInfoMapper.deleteTbProductInfoByProIds(proIds);
    }

    @Override
    public List<AppProductOptionData> getAppOptionData(String name) {
        return tbProductInfoMapper.selectProductOptionData(name);
    }

    @Override
    public List<AppProductOptionData> getPopularProducts() {
        return tbProductInfoMapper.selectPopularProducts();
    }
}
